const app = getApp();
var api = require('../../../../api.js');
var mta = require('../../../../mta_analysis.js');

Page({
  data: {
    value: 1,
    id: 0,
    list:{
      card: '黄金会员卡',
      number: 9,
      judge:false
    }
  },
  onLoad: function (options) {
    console.log(options)
    app.bind();
    var that = this;
    that.setData({
      id: options.id
    });

    mta.Page.init();

    that.getNumber();

  },
  onReady: function () {
  
  },
  onShow: function () {
    this.getNumber()
  },
  onHide: function () {
  
  },
  onUnload: function () {
  
  },
  onPullDownRefresh: function () {
  
  },
  onReachBottom: function () {
  
  },
  // 分享
  onShareAppMessage: function () {
    var that = this;
    wx.hideShareMenu();

    var url = "pages/user/card/receive/receive";
    var user_id = wx.getStorageSync('user_id');
    var id = that.data.newId;
    var card = that.data.list.gold_card_url;
    console.log(url + "?id=" + id + "&user_id=" + user_id);
    console.log(card)

    return {
      path: url + "?id=" + id + "&user_id=" + user_id,
      title: '赠送你一张1314元的黄金会员学习卡，赶快点击领取哦',
      imageUrl: card,
      success: function () {

      }
    };  

    

    


    
  },
  // 获取可转赠会员卡数量
  getNumber: function () {
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    wx.request({
      url:api.card.number,
      method: "post",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        id: that.data.id
      },
      success: function (e) {
        console.log(e)
        var list = e.data.data;
        if (e.data.code == 0) {
          that.setData({
            list: list
          })
          console.log(that.data.list)
        } else {
          wx.navigateTo({
            url: '/pages/grant/grant'
          })
        }
      }
    });
  },


  // 确认转赠
  transferSend: function () {
    var that = this;
    var access_token = wx.getStorageSync('access_token');

    wx.request({
      url: api.card.transfer,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        id: that.data.id,
        share_num: that.data.value,
      },
      success: function (e) {
        console.log(e);
        if (e.data.code == 0) {
          that.setData({
            newId: e.data.data,
            judge: true,
          })
          console.log(that.data.newId)



        } else {

        }

      }
    });
  },


  // 取消
  cancelDo: function (res) {
    
    var page = this;
    var access_token = wx.getStorageSync('access_token');

    page.setData({
      judge: false
    })

    wx.request({
      url: api.index.cancelShare,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        id: page.data.newId
      },

      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          // wx.showToast({
          //   title: res.data.msg,
          //   icon: 'none',
          // })
          // page.getDetail();

        } else if (res.data.code == -1) {
          wx.navigateTo({
            url: '/pages/grant/grant',
          })
        }



      }
    })
  },

  changJudge:function(){
    var page = this;
    page.setData({
      judge: false
    })
  },



  //改变输入框内的数量
  numberBlur: function (e) {
    console.log(e.detail.value)
    var that = this;
    var maxNum = that.data.list.left_num;
    var num = e.detail.value;
    num = parseInt(num);
    
    if (isNaN(num)){
      num = 1;
    } else {
      if (num <= 0){
        that.setData({
          value: 1,
        })
      } else if (num >= maxNum) {
        that.setData({
          value: maxNum
        })
      } else {
        that.setData({
          value: num
        })
      }
    }
    // console.log(that.data.value);
  },
})